package day16;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;

public class TcpClient {
    public static void main(String[] args) throws UnknownHostException, IOException{
        //创建客户端对象，连接服务器
        //指定服务器ip 端口
        //注意这里的ip和端口都是服务器定义好的
        //企业级专线 固定ip
        Socket client = new Socket("127.0.0.1",8888);
        System.out.println("客户端："+client);
        //获取IO流
        InputStream is = client.getInputStream();
        OutputStream os = client.getOutputStream();
        //给服务器发送数据
        String string = "Hello,server,我是Client";
        //字符串转byte类型数组
        byte[] b = string.getBytes();
        os.write(b);
        //客户端接收 - 解析服务器的消息
        byte[] b1 = new byte[1024];
        int length = is.read(b1);
        if (length != -1) {
            //输出读到的内容数组 b1
            System.out.println(new String(b1,0,length));
        }
        //关闭资源 - 注意处理异常 - 
        os.close();
        is.close();
        client.close();
    }
}
